<template>
  <div class="container">
    <scroll-view :scroll-y="true"
                 :lower-threshol="150"
                 :style="{'height': '100%'}">
      <a class="notice"
         href="/pages/package_mine/activationTicket/main">
        <div class="icon">
          <img :src="base_url+'/mp_2.5/laba.png'"
               v-if="base_url"
               mode="widthFix"
               alt
               class="img" />
        </div>
        <div class="msg">点击此处立即激活会员卡(无实体卡请忽略)</div>
      </a>
      <div class="top clearfix">
        <a href="/pages/package_mine/card_disabled/main"
           class="disabled">失效卡券</a>
      </div>

      <div class="section card_section">
        <div class="header clearfix">
          <div class="left">
            <div class="icon">
              <img :src="base_url+'/mp_2.4/card/ka.png'"
                   v-if="base_url"
                   alt
                   class="img" />
            </div>
            <div class="msg"
                 v-if="card_length<=3&&card_length>0">卡({{card_length}})</div>
            <div class="msg"
                 v-else>卡</div>
          </div>
          <div class="right">
            <div class="empty-btn"
                 v-if="card_length==0">暂无可用</div>
            <a href="/pages/package_mine/card_list/main"
               class="more-btn"
               v-if="card_length>3">
              查看全部 ({{card_length}})
              <div class="icon">
                <img :src="base_url+'/jifenshangcheng/xiangqingye/xiayibu@2x.png'"
                     v-if="base_url"
                     mode="widthFix"
                     class="img" />
              </div>
            </a>
          </div>
        </div>
        <ul class="card-list clearfix">
          <li class="list-item"
              :class="{
                'super-vip':item.card_type==0||item.card_type==5,
                'project':item.card_type==1,
                'friends':item.card_type==2,
                'partner':item.card_type==3,
                'black-gold':item.card_type==4
                }"
              v-for="(item, index) in card_arr"
              :key="index"
              @tap="card_detail(item)">
            <div class="title">
              <div class="msg">
                {{item.card_type==0||item.card_type==5?'超级会员卡':''}}
                {{item.card_type==1?'项目卡':''}}
                {{item.card_type==2?'亲友卡':''}}
                {{item.card_type==3?'加盟卡':''}}
                {{item.card_type==4?'黑金卡':''}}
              </div>
              <div class="line"></div>
              <div class="logo">
                <img :src="base_url+'/mp_2.4/card/logo.png'"
                     v-if="base_url"
                     mode="widthFix"
                     alt
                     class="img" />
              </div>
            </div>
            <div class="price">
              <span class="icon">￥</span>
              <span class="num">{{item.price}}</span>
            </div>
          </li>
        </ul>
      </div>
      <div class="section ticket-section">
        <div class="header clearfix">
          <div class="left">
            <div class="icon">
              <img :src="base_url+'/mp_2.4/card/youhuiquan-2@2x.png'"
                   v-if="base_url"
                   alt
                   class="img" />
            </div>
            <div class="msg"
                 v-if="ticket_length<=3&&ticket_length>0">优惠券({{ticket_length}})</div>
            <div class="msg"
                 v-else>优惠券</div>
          </div>
          <div class="right">
            <div class="empty-btn"
                 v-if="ticket_length==0">暂无可用</div>
            <a href="/pages/package_mine/ticket_list/main"
               class="more-btn"
               v-if="ticket_length>3">
              查看全部 ({{ticket_length}})
              <div class="icon">
                <img :src="base_url+'/jifenshangcheng/xiangqingye/xiayibu@2x.png'"
                     v-if="base_url"
                     mode="widthFix"
                     class="img" />
              </div>
            </a>
          </div>
        </div>
        <ul class="ticket-list clearfix">
          <li class="ticket"
              :class="{'mask-used':type!=1,'disabled':type==3}"
              v-for="(item, index) in ticket_arr"
              :key="index">
            <div class="left">
              <!-- 满减券 -->
              <div class="center"
                   v-if="item.source!=3">
                <div class="money">
                  <div class="icon">￥</div>
                  <div class="num">{{item.type_money}}</div>
                </div>
                <div class="tip">满{{item.type_min_money}}可用</div>
              </div>
              <!-- 特殊券 -->
              <div class="center special"
                   v-if="item.source==3">体验券</div>
            </div>
            <div class="right">
              <div class="t-title clearfix">
                <div class="t-type">[{{item.source_name}}]</div>
                <div class="t-name">{{item.type_name}}</div>
              </div>
              <div class="dec">{{item.condtion_name}}</div>
              <div class="timer">{{item.start}}~{{item.end}}</div>
            </div>
            <div class="user-btn"
                 @tap.stop="use_ticket (item)">
              <img :src="base_url+'/mp_3.0/mine/user_btn@2x.png'"
                   v-if="base_url"
                   alt
                   class="img" />
            </div>
          </li>
        </ul>
      </div>
    </scroll-view>
  </div>
</template>

<script>
import common from "@/assets/js/mmk_common.js";
export default {
  data () {
    return {
      base_url: common.image_resource,
      img_url: common.image_response,
      card_arr: [],
      ticket_arr: [],
      card_length: 0,
      ticket_length: 0
    };
  },
  onLoad (options) {
    wx.setNavigationBarTitle({
      title: "卡券中心"
    });
  },
  onShow () {
    this.get_data();
  },
  methods: {
    get_data () {
      common.fly_post("api/v4_5/card_ticket/list", {}, response => {
        let res = response.data;
        if (res.status_code == 0) {
          res.data.ticket.forEach(item => {
            item.start = new Date(item.send_start_time * 1000).Format(
              "yyyy.MM.dd"
            );
            item.end = new Date(item.send_end_time * 1000).Format("yyyy.MM.dd");
          });
          let arr_card = res.data.card;
          let arr_ticket = res.data.ticket;
          this.card_length = arr_card.length;
          this.ticket_length = arr_ticket.length;
          if (this.card_length > 3) {
            this.card_arr = res.data.card.slice(0, 3);
          } else {
            this.card_arr = res.data.card;
          }
          if (this.ticket_length > 3) {
            this.ticket_arr = res.data.ticket.slice(0, 3);
          } else {
            this.ticket_arr = res.data.ticket;
          }
        }
      });
    },
    card_detail (item) {
      wx.navigateTo({
        url: `/pages/package_mine/card_detail/main?id=${item.id}`
      });
    },
    use_ticket (item) {
      //   condition 1--项目 2--商品
      if (item.jump_link != "") {
        let json_data = JSON.parse(item.jump_link) || {};
        switch (json_data.type) {
          case 1:
            // 跳转分类
            switch (item.condtion) {
              case 1:
                wx.navigateTo({
                  url: `/pages/package_projects/categorize_list/main?url=${
                    json_data.id
                    }&type=${json_data.type}`
                });
                break;
              case 2:
                wx.navigateTo({
                  url: `/pages/package_projects/shop_type_detail/main?id=${
                    json_data.id
                    }&type=${json_data.type}`
                });
                break;
              case 3:
                // let router = decodeURIComponent(`serviceDetail?serviceId=${json_data.id}&shopId=${json_data.shop_id}`)
                wx.navigateTo({ url: "/pages/life_beauty/main" });
                break;
              default:
                break;
            }

            break;
          case 2:
            // 跳转二级分类（只有商品有）
            wx.navigateTo({
              url: `/pages/package_projects/shop_type_detail/main?id=${
                json_data.id
                }&type=${json_data.type}`
            });
            break;
          case 3:
            //  跳转指定项目或商品
            switch (item.condtion) {
              case 1:
                wx.navigateTo({
                  url: `/pages/package_projects/project_detail/main?id=${
                    json_data.id
                    }`
                });
                break;
              case 2:
                wx.navigateTo({
                  url: `/pages/package_mall/goods_detail/main?goods_id=${
                    json_data.id
                    }`
                });
              case 3:
                wx.navigateTo({ url: "/pages/life_beauty/main" });
                break;
              default:
                break;
            }
            break;
          case 4:
            //  跳转可用商品、项目列表
            switch (item.condtion) {
              case 1:
                wx.navigateTo({
                  url: `/pages/package_mine/ticket_projects/main?id=${
                    item.ticket_id
                    }`
                });
                break;
              case 2:
                wx.navigateTo({
                  url: `/pages/package_mine/ticket_goods/main?id=${
                    item.ticket_id
                    }`
                });
                break;
              case 3:
                wx.navigateTo({ url: "/pages/life_beauty/main" });
                break;
              default:
                break;
            }
            break;
          default:
            // 没有这个字段就默认跳转首页
            if (item.condtion == 1) {
              wx.switchTab({ url: "/pages/beauty_mall/main" });
            } else if (item.condtion == 2) {
              wx.switchTab({ url: "/pages/shop_mall/main" });
            } else if (item.condtion == 3) {
              wx.navigateTo({ url: "/pages/life_beauty/main" });
            }
            break;
        }
      } else {
        if (item.condtion == 1) {
          wx.switchTab({ url: "/pages/beauty_mall/main" });
        } else if (item.condtion == 2) {
          wx.switchTab({ url: "/pages/shop_mall/main" });
        } else if (item.condtion == 3) {
          wx.navigateTo({ url: "/pages/life_beauty/main" });
        } else if (item.condtion == 4) {
          // 女王卡
          wx.navigateTo({
            url:
              "/pages/package_main/advertisement/main?url=" +
              encodeURIComponent(item.url)
          });
        } else if (item.condtion == 7) {
          //邀请有礼（单项目->邀请有礼确认预约 多项目->邀请有礼选择项目医院）
          let good_idNum =
            (item.good_id && item.good_id.split(",").length) || 0;
          let ticket_id = this.GetQueryString("ticket_id", item.url);
          let project_id = this.GetQueryString("id", item.url);
          good_idNum > 1
            ? wx.navigateTo({
              url: `/pages/package_projects/select_project_hospital/main?ticket_id=${
                ticket_id
                }`
            })
            : wx.navigateTo({
              url: `/pages/package_projects/confirm_appoint/main?ticket_id=${
                ticket_id
                }&project_id=${project_id}`
            });
        }
      }
    },
    // 获取url参数
    GetQueryString (name, url) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = url.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    toActivationTicket () {
      wx.navigateTo({ url: `/pages/activationTicket/main` });
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  background: rgba(243, 244, 246, 1);
  .notice {
    height: 40px;
    background: rgba(255, 223, 223, 1);
    padding-left: 15px;
    .icon {
      display: inline-block;
      vertical-align: middle;
      width: 16px;
      margin-right: 5px;
      .img {
        width: 100%;
        display: block;
      }
    }
    .msg {
      display: inline-block;
      vertical-align: middle;
      font-size: 13px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      line-height: 40px;
    }
  }
  .top {
    height: 40px;
    line-height: 40px;
    padding-right: 15px;
    .disabled {
      float: right;
      font-size: 14px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
    }
  }
  .section {
    background-color: #fff;
    margin-bottom: 10px;
    padding: 0 15px;
    .header {
      height: 60px;
      width: 100%;
      .left {
        float: left;
        .icon {
          width: 25px;
          height: 20px;
          display: inline-block;
          vertical-align: middle;
          .img {
            width: 100%;
            height: 100%;
            display: block;
          }
        }
        .msg {
          display: inline-block;
          vertical-align: middle;
          line-height: 60px;
          font-size: 14px;
          margin-left: 5px;
          font-family: "PingFangSC-Regular";
          font-weight: 400;
          color: rgba(51, 51, 51, 1);
        }
      }
      .right {
        float: right;
        font-size: 14px;
        font-family: "PingFangSC-Regular";
        font-weight: 400;
        color: rgba(153, 153, 153, 1);
        line-height: 60px;
        .icon {
          width: 20px;
          display: inline-block;
          vertical-align: middle;
          margin-left: -5px;
          .img {
            width: 100%;
            display: block;
          }
        }
      }
    }
  }
  .card_section {
    .card-list {
      .list-item {
        height: 100px;
        background-size: 100% 100%;
        background-repeat: no-repeat;
        position: relative;
        margin-bottom: 10px;
        .title {
          position: absolute;
          top: 15px;
          left: 15px;
          .msg {
            font-size: 22px;
            font-family: "PingFangSC-Medium";
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
            display: inline-block;
            vertical-align: middle;
          }
          .line {
            width: 1px;
            height: 18px;
            background: rgba(255, 255, 255, 1);
            opacity: 0.6022;
            display: inline-block;
            vertical-align: middle;
            margin: 0 10px;
          }
          .logo {
            width: 67px;
            display: inline-block;
            vertical-align: middle;
            .img {
              width: 100%;
            }
          }
        }
        .price {
          position: absolute;
          bottom: 10px;
          left: 15px;
          .icon {
            font-size: 17px;
            font-family: "PingFangSC-Medium";
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
            vertical-align: bottom;
            display: inline-block;
          }
          .num {
            font-size: 25px;
            font-family: "PingFangSC-Medium";
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
            vertical-align: bottom;
            display: inline-block;
            line-height: 30px;
            margin-left: -1px;
          }
        }
        &:last-of-type {
          margin-bottom: 20px;
        }
      }
      //   项目卡
      .project {
        background-image: url(https://h5.ameimeika.com/mp_images/mp_2.4/card/xiangmuxiao.png);
      }
      //   亲友卡
      .friends {
        background-image: url(https://h5.ameimeika.com/mp_images/mp_2.4/card/qinyouxiao.png);
      }
      //   加盟卡
      .partner {
        background-image: url(https://h5.ameimeika.com/mp_images/mp_2.4/card/jiamengxiao.png);
      }
      //   超级会员卡
      .super-vip {
        background-image: url(https://h5.ameimeika.com/mp_images/mp_2.4/card/chaoji小.png);
      }
      //   黑金卡
      .black-gold {
        background-image: url(https://h5.ameimeika.com/mp_images/mp_2.4/card/heijxiao.png);
        .title {
          .msg {
            font-size: 22px;
            font-family: "PingFangSC-Medium";
            font-weight: 500;
            color: rgba(219, 177, 120, 1);
            display: inline-block;
            vertical-align: middle;
          }
        }
      }
    }
  }
  .ticket-section {
    .ticket-list {
      .ticket {
        display: flex;
        height: 92px;
        margin: 10px 0;
        background-color: #fff;
        border-radius: 10px;
        box-shadow: 0px 2px 8px 0px rgba(164, 178, 193, 0.45);
        overflow: hidden;
        position: relative;
        .left {
          width: 98px;
          height: 100%;
          border-right: 1px dashed rgba(235, 235, 235, 1);
          position: relative;
          .center {
            width: 100%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            .money {
              text-align: center;
              .icon {
                font-size: 12px;
                font-family: "STHeitiTC-Medium", "STHeitiTC";
                font-weight: 500;
                color: rgba(220, 47, 107, 1);
                display: inline-block;
                vertical-align: bottom;
              }
              .num {
                font-size: 26px;
                font-family: "STHeitiTC-Medium", "STHeitiTC";
                font-weight: 500;
                color: rgba(220, 47, 107, 1);
                line-height: 24px;
                display: inline-block;
                vertical-align: bottom;
              }
            }
            .tip {
              font-size: 12px;
              font-family: "PingFangSC-Regular", "PingFang SC";
              font-weight: 400;
              color: rgba(235, 118, 158, 1);
              line-height: 17px;
              text-align: center;
              margin-top: 6px;
            }
            &.special {
              font-size: 18px;
              font-family: "PingFangSC-Semibold", "PingFang SC";
              font-weight: 600;
              color: rgba(220, 47, 107, 1);
              line-height: 24px;
              text-align: center;
            }
          }

          &::before {
            content: "";
            width: 10px;
            height: 10px;
            border-radius: 50%;
            display: block;
            background-color: #fff;
            box-shadow: 0px 2px 8px 0px inset rgba(164, 178, 193, 0.45);
            position: absolute;
            top: 0;
            right: 0;
            transform: translate(50%, -50%);
          }
          &::after {
            content: "";
            width: 10px;
            height: 10px;
            border-radius: 50%;
            display: block;
            background-color: #fff;
            box-shadow: 0px 2px 8px 0px inset rgba(164, 178, 193, 0.45);
            position: absolute;
            bottom: 0;
            right: 0;
            transform: translate(50%, 50%);
          }
        }
        .right {
          flex: 1;
          padding: 10px;
          box-sizing: border-box;
          position: relative;
          .t-title {
            font-size: 16px;
            font-family: "PingFangSC-Medium", "PingFang SC";
            font-weight: 500;
            line-height: 20px;
            display: flex;
            .t-type {
              color: rgba(254, 120, 158, 1);
              vertical-align: middle;
              display: inline-block;
            }
            .t-name {
              color: rgba(29, 32, 35, 1);
              vertical-align: middle;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 1;
              -webkit-box-orient: vertical;
              flex: 1;
            }
          }
          .dec {
            font-size: 12px;
            font-family: "PingFangSC-Regular", "PingFang SC";
            font-weight: 400;
            color: rgba(63, 69, 75, 1);
            line-height: 16px;
            margin-top: 5px;
            width: 80%;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
          }
          .timer {
            font-size: 12px;
            font-family: "PingFangSC-Regular", "PingFang SC";
            font-weight: 400;
            color: rgba(96, 105, 114, 1);
            line-height: 16px;
            position: absolute;
            left: 10px;
            bottom: 10px;
          }
        }
        .user-btn {
          width: 52px;
          height: 52px;
          position: absolute;
          bottom: 5px;
          right: 7px;
          .img {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
}
</style>


